package com.huayun.plugin.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class R<T> {
    private String message;
    private T data;
    private long code;

    public boolean isSuccess(){
        return code == RCode.SUCCESS.getCode();
    }

    public static <T> RBuilder<T> builder() {
        return new RBuilder<>();
    }

    public static class RBuilder<T> {
        private String message;
        private T data;
        private long code;

        RBuilder() {
        }

        public RBuilder<T> success(){
            message(RCode.SUCCESS.getMessage());
            code(RCode.SUCCESS.getCode());
            return this;
        }

        public RBuilder<T> error(){
            message(RCode.ERROR.getMessage());
            code(RCode.ERROR.getCode());
            return this;
        }

        public RBuilder<T> message(final String message) {
            this.message = message;
            return this;
        }

        public RBuilder<T> data(final T data) {
            this.data = data;
            return this;
        }

        public RBuilder<T> code(final long code) {
            this.code = code;
            return this;
        }

        public R<T> build() {
            return new R<>(this.message, this.data, this.code);
        }

        public String toString() {
            return "R.RBuilder(message=" + this.message + ", data=" + this.data + ", code=" + this.code + ")";
        }
    }
}